package insight.google.game.era.data;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Key;

public class UserDataManager extends AppDataManager {

	
	public UserDataManager() {
		this.isDetachable = true;
		super.initialize();
	}
	
	public UserDataManager(TransactionHandler txHandler) {
		this.txHandler = txHandler;
		super.initialize();
	}
	
	
	
	public UserDataManager(TransactionHandler txHandler,boolean isDetachable) {
		this.txHandler = txHandler;
		this.isDetachable = isDetachable;
		super.initialize();
	}

//	@Override
//	public boolean addToDataStore(Object o) {
//		try {
//			dataStoreMgr.save(o, pManager,this.isDetachable);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (Exception ex) {
//
//		}
//		return false;
//	}

	@Override
	public ArrayList<User> findAll() {
		List<User> userList = null;
		try {
			userList = (List<User>) dataStoreMgr.get(User.class, pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ArrayList<User>(userList);
		// return null;
	}

	public User findByName(String userName) {
		StringBuffer cond = new StringBuffer();
		cond.append("name == '" + userName + "'");
		return getSingleUser(cond.toString());

	}

	public User findByGoogleId(com.google.appengine.api.users.User googUser) {
		StringBuffer cond = new StringBuffer();
		cond.append("email == '" + googUser.getEmail() + "'");
		return getSingleUser(cond.toString());
	}

	public User findById(Key userId) {
		return (User) DataStoreManager.getInstance().getById(User.class,
				userId, pManager,this.isDetachable);
		// StringBuffer cond = new StringBuffer();
		// cond.append("id == '" + userId + "'");
		// return getSingleUser(cond.toString());

	}
	
	public List<User> findAllLoggedInUser() {
		StringBuffer cond = new StringBuffer();
		cond.append("isLoggedIn == true");
		List<User> list = null;
		try {
			list = (List<User>) DataStoreManager.getInstance().getByCondition(
					User.class, cond.toString(),
					pManager,
					this.isDetachable
					);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}

	public List<User> findAllByRoomId(Key roomId) {
		StringBuffer cond = new StringBuffer();
		cond.append("roomId == :refId");
		List<User> list = null;
		try {
			list = (List<User>) DataStoreManager.getInstance().getByConditionWithKeyParam(
					User.class, cond.toString(),
					pManager,
					roomId,this.isDetachable
					);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}

	private User getSingleUser(String cond) {
		User user = null;
		List<User> userList = null;
		try {
				userList = DataStoreManager.getInstance().getByCondition(User.class, cond, pManager,this.isDetachable);
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (userList != null) {
			for (int i = 0; i < userList.size(); i++) {
				user = (User) userList.get(i);
				break;
			}
		}
		return user;
	}

	// @Override
	// public boolean removeDataStore() {
	// return DataStoreManager.getInstance().delete(user);
	//		
	// }

	public boolean removeDataStore(User user) {
		return DataStoreManager.getInstance().delete(user,pManager);

	}

}
